


#############################################

      Table A7. High Factor Loadings

#############################################

      N=100

###################

      
library(lavaan)
library(boot)      
      

identity<-diag(1, 24,24)
phi<-matrix(c( 1, 0.3, 0.4,
               0.3, 1, 0.5,
               0.4, 0.5, 1
), 3,3)

ld<-matrix(c(0.75, 0.8, 0.8, 0.8, 0.8, 0.75, 0.8, 0.85, 0.8, 0, 0, 0, 0, 0, 0,0,  0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0.65, 0, 0,  0.7, 0.75, 0.8, 0.8, 0.8, 0.8, 0.8, 0.75, 0, 0, 0, 0.8, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0.65,  0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.7, 0.7 
), nrow=24, ncol=3)

ld2<-t(ld)
psi<-diag(1,24)-diag(ld %*% phi %*% ld2)
diag(psi)




popmodel<-'
f1 =~ 0.8*x1 + 0.8*x2 + 0.8*x3 + 0.8*x4 + 0.8*x5 + 0.9*x6 + 0.9*x7 + 0.85*x8 + 0.9*x9
f2 =~ 0.8*x9 + 0.8*x10 + 0.8*x11 + 0.8*x12 + 0.8*x13 + 0.8*x14 + 0.8*x15 + 0.85*x16 + 0.9*x6 + 0.9*x20
f3 =~ 0.8*x17 + 0.8*x18 + 0.8*x19 + 0.8*x20 + 0.9*x21 + 0.9*x22 + 0.8*x23 + 0.8*x24 + 0.9*x16

f3 ~~ 0.4*f1 + 0.5*f2
f1 ~~ 0.3*f2

f1 ~ 0*1
f2 ~ 0*1
f3 ~ 0*1

x1 ~~ 0.96*x1
x2 ~~ 0.96*x2

x3 ~~ 0.91*x3
x4 ~~ 0.91*x4
x5 ~~ 0.91*x5
x6 ~~ 0.5295*x6

x7 ~~ 0.84*x7
x8 ~~ 0.8775*x8
x9 ~~ 0.7035*x9

x10 ~~  0.96*x10
x11 ~~  0.96*x11

x12 ~~ 0.91*x12
x13 ~~ 0.91*x13
x14 ~~ 0.8775*x14
x15 ~~ 0.5775*x15

x16 ~~ 0.5775*x16
x17 ~~ 0.96*x17

x18 ~~ 0.91*x18
x19 ~~ 0.91*x19
x20 ~~ 0.2925*x20
x21 ~~ 0.84*x21
x22 ~~ 0.84*x22

x23 ~~ 0.91*x23
x24 ~~ 0.91*x24


'

mis_model<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24
f3 ~~ f1
f1 ~~ f2


'


set.seed(100)
mydata<-simulateData(popmodel, model.type = "sem", sample.nobs=100L)
fit1 <- sem(model = mis_model, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")



modindices(fit1, minimum.value = 0, sort = TRUE)[1:12,]

newpar = '
f3	=~	x16
f2	=~	x6
f2	=~	x20
f1	=~	x9
f3	=~	x6
x3	~~	x9
f2	=~	x21
f1	=~	x12
x10	~~	x16
x12	~~	x20
x16	~~	x17
x12	~~	x21

'

lavTestScore(fit1, add=newpar)



##############. Bootstrap LM Test


newpar = '
f3=~x16
f2=~x6
f2=~x20
f1=~x9
f3=~x6
x9~~x3
f2=~x21
f1=~x12
x16~~x10
x20~~x12
x16~~x17
x21~~x12'



set.seed(100)
Data<-mydata
# Initialize the matrix to store bootstrap results
bin.1000 <- matrix(NA, nrow = 500, ncol = 2)

# Loop through bootstrap iterations
for (i in 1:500) {
  
  # Perform one bootstrap iteration
  boot.res <- tryCatch({
    # Generate bootstrap sample
    boot.idx <- sample.int(nrow(Data), replace = TRUE)
    Data.boot <- Data[boot.idx, ]
    
    # Fit the SEM model
    fit2 <- sem(model = mis_model, data = Data.boot, int.ov.free = FALSE, std.lv = FALSE, estimator = "ML")
    
    # Calculate scores
    score1 <- lavTestScore(fit2, add = newpar)$uni[1, 4]
    score2 <- lavTestScore(fit2, add = newpar)$uni[1, 6]
    
    # Return scores
    list(score1 = score1, score2 = score2)
  }, error = function(e) {
    # If SEM model did not converge, return NULL
    NULL
  })
  
  # Check if boot.res is not NULL (i.e., SEM model converged)
  if (!is.null(boot.res)) {
    # Update the matrix with scores
    bin.1000[i, 1] <- boot.res$score1
    bin.1000[i, 2] <- boot.res$score2
  }
}

# Calculate the means of the scores
t1 <- mean(bin.1000[, 1], na.rm = TRUE)
t2 <- mean(bin.1000[, 2], na.rm = TRUE)
t1
t2


#######  Bootstrap Wald Test

w_model<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + b4*x9 + b8*x12
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + b2*x6 + b3*x20 + b7*x21
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + b1*x16 + b5*x6
f3 ~~ f1
f1 ~~ f2

x9~~b6*x3
x16~~b9*x10
x20~~b10*x12
x16~~b11*x17
x21~~b12*x12

'



fit2 <- sem(model = w_model, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


con='

b1==0'



set.seed(100)
Data<-mydata
# Initialize the matrix to store bootstrap results
bin.1000 <- matrix(NA, nrow = 500, ncol = 2)

# Loop through bootstrap iterations
for (i in 1:500) {
  
  # Perform one bootstrap iteration
  boot.res <- tryCatch({
    # Generate bootstrap sample
    boot.idx <- sample.int(nrow(Data), replace = TRUE)
    Data.boot <- Data[boot.idx, ]
    
    # Fit the SEM model
    fit2 <- sem(model = w_model, data = Data.boot, meanstructure = FALSE, likelihood = "wishart", estimator = "ML")
    
    # Calculate Wald test statistics and p-values
    wald_stat <- lavTestWald(fit2, constraints = con)$stat[1]
    wald_p_value <- lavTestWald(fit2, constraints = con)$p.value[1]
    
    # Return statistics
    list(stat = wald_stat, p_value = wald_p_value)
  }, error = function(e) {
    # If SEM model did not converge, return NULL
    NULL
  })
  
  # Check if boot.res is not NULL (i.e., SEM model converged)
  if (!is.null(boot.res)) {
    # Update the matrix with Wald test statistics and p-values
    bin.1000[i, 1] <- boot.res$stat
    bin.1000[i, 2] <- boot.res$p_value
  }
}

# Calculate the means of the Wald test statistics and p-values
w1 <- mean(bin.1000[, 1], na.rm = TRUE)
w2 <- mean(bin.1000[, 2], na.rm = TRUE)
w1
w2





###############################################################

        Likelihood Ratio Test

###############################################################

m_0<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24
f3 ~~ f1
f1 ~~ f2

'
lrt_0<-sem(model = m_0, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


m_1<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16
f3 ~~ f1
f1 ~~ f2

'
lrt_1<-sem(model = m_1, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


m_2<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16
f3 ~~ f1
f1 ~~ f2

'
lrt_2<-sem(model = m_2, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")



m_3<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16
f3 ~~ f1
f1 ~~ f2

'
lrt_3<-sem(model = m_3, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


m_4<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16
f3 ~~ f1
f1 ~~ f2

'
lrt_4<-sem(model = m_4, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


m_5<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

'
lrt_5<-sem(model = m_5, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")



m_6<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

x9~~x3

'
lrt_6<-sem(model = m_6, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")



m_7<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20 + x21
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

x9~~x3

'
lrt_7<-sem(model = m_7, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


m_8<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9 + x12
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20 + x21
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

x9~~x3
'


lrt_8<-sem(model = m_8, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


m_9<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9 + x12
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20 + x21
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

x9~~x3
x16~~x10
'


lrt_9<-sem(model = m_9, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")



m_10<-'  

f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9 + x12
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20 + x21
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

x9~~x3
x16~~x10
x20~~x12

'

lrt_10<-sem(model = m_10, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


m_11<-' 
f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9 + x12
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20 + x21
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

x9~~x3
x16~~x10
x20~~x12
x16~~x17

 '
lrt_11<-sem(model = m_11, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")



m_12<-' 
f1 =~ x1 + x2 + x3 + x4 + x5 + x6  + x7 + x8 + x9 + x12
f2 =~ x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x6 + x20 + x21
f3 =~ x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x16 + x6
f3 ~~ f1
f1 ~~ f2

x9~~x3
x16~~x10
x20~~x12
x16~~x17
x21~~x12
 '

lrt_12<-sem(model = m_12, data=mydata, meanstructure=FALSE, likelihood = "wishart", estimator = "ML")


lavTestLRT(lrt_0, lrt_1, lrt_2, lrt_3, lrt_4, lrt_5, lrt_6, lrt_7, lrt_8, lrt_9, lrt_10, lrt_11, lrt_12)



##########. The End

########## All results match the manuscript, 12/4/24




